package com.tanhua.server.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {
    @Reference
    private BlackListApi blackListApi;
    @Reference
    private SettingsApi settingsApi;
    @Reference
    private QuestionApi questionApi;

    //通用设置查询信息
    public ResponseEntity settings() {
        SettingsVo vo=new SettingsVo();
        //1.首先设置用户id,然后设置用户的手机号码
        User user = UserHolder.getUser();//从threadLocal中获取User用户
        vo.setId(user.getId());
        vo.setPhone(user.getMobile());

        //2.查询当前用户的陌生人问题(这都是根据用户id来查询的)
        Question question=questionApi.findByUserId(user.getId());
        vo.setStrangerQuestion(question!=null?question.getTxt():"你喜欢java吗?");
        //3.查询当前用户的通知配置(这都是根据用户id来查询的)
        Settings settings=settingsApi.findByUserId(user.getId());
        if (settings!=null){
            BeanUtils.copyProperties(settings,vo);
        }
        //4.构建返回值
        return ResponseEntity.ok(vo);
    }

    //保存或者更新陌生人问题
    public ResponseEntity questions(String content) {
        //1.获取当前用户Id
        Long userId = UserHolder.getUserId();
        //2.根据当前用户id获取陌生人问题
        Question question = questionApi.findByUserId(userId);
        if (question != null) {
            //2.1如果陌生人问题存在 那就进行覆盖更新
            question.setTxt(content);
            questionApi.update(question);
        } else {
            //2.2如果陌生人问题不存在,那就进行创建赋值
            question=new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);

            }
        //3.返回值
        return ResponseEntity.ok(null);
    }

    //保存或者更新用户设置
    public ResponseEntity saveSettings(Settings settings) {
        //1.获取用户对象id
        Long userId = UserHolder.getUserId();
        //2.根据用户对象查询数据
        Settings settings1=settingsApi.findByUserId(userId);

        if (settings1!=null){
            //2.1对象存在数据,就进行覆盖更新
            settings1.setGonggaoNotification(settings.getGonggaoNotification());
            settings1.setLikeNotification(settings.getLikeNotification());
            settings1.setPinglunNotification(settings.getPinglunNotification());
            settingsApi.update(settings1);
        } else {
            //2.2对象不存在数据,就进行保存
            settings.setUserId(userId);
            settingsApi.save(settings);
        }
        //3.返回
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单
     * 1.查询的数据库表单是userInfo 条件是user的id=指定数 所引导出的黑名单id 再查询黑名单id在userInfo里面
     * SELECT * FROM tb_user_info WHERE id IN(SELECT black_user_id FROM tb_black_list WHERE user_id=107)
     * 2.查询的参数是什么
     * user的id 分页的参数
     * 3.mybatis-plus调用分页查询之后的返回值是什么
     * Ipage(接口):page
     * @param page
     * @param pagesize
     * @return
     */
    public ResponseEntity blacklist(Integer page, Integer pagesize) {
        //1.首先获取用户id
        Long userId = UserHolder.getUserId();
        //2.调用API分页查询.Ipage<userinfo>(mybatis-plus内置的对象)
        IPage<UserInfo> iPage=blackListApi.findPage(page,pagesize,userId);
        //3.构造PageResult
        PageResult result=new PageResult(page,pagesize,(int)iPage.getTotal(),iPage.getRecords());
        //4.返回值
        return  ResponseEntity.ok(result);
    }

    //移除黑名单用户 blackUserId 黑名单用户Id
    public ResponseEntity deleteBlackList(Long blackUserId) {
        //1.首先获取操作用户的id
        Long userId = UserHolder.getUserId();
        //2.调用API删除
        blackListApi.delete(userId,blackUserId);
        //3.删除完成 构造返回值
        return ResponseEntity.ok(null);
    }
}
